<?php
header("Content-type: text/html; charset=utf-8");
class Net_model extends CI_Model{
	function __construct()
    {
        parent::__construct();
    }
	function dhcp_view($sql){ //显示主页面的内容
		
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	function dhcp_file_view($id){ //显示修改页面的内容
		//$this->id = $id;
		$sql = "select * from dhcp where id='{$id}'";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}
	function new_dhcp(){ //新建
		$sql = "select * from dhcp order by id desc";
		$host = $this->input->post('host');
		$mac = $this->input->post('mac');
		$ip = $this->input->post('ip');
		$name = $this->input->post('name');
		$computer = $this->input->post('computer');
		$unit = $this->input->post('unit');
		$note = $this->input->post('note');
		$query = $this->db->query($sql);
		foreach($query->result_array() as $row){
			if($row['host']==$host){
				echo "<script language=javascript>
				alert('主机名已存在');
				window.location='".base_url()."net_dhcp/newfile';
				</script>";
				exit();
			}elseif($row['mac']==$mac){
				echo "<script language=javascript>
					alert('mac地址已存在');
					window.location='".base_url()."net_dhcp/newfile';
					</script>";	
					exit();
			}elseif($row['ip']==$ip){
				echo "<script language=javascript>
					alert('ip地址已存在');
					window.location='".base_url()."net_dhcp/newfile';
					</script>";	
					exit();
			}
		}
		$query = $this->db->query("insert into dhcp values('default','{$host}','{$mac}','{$ip}','{$name}','{$computer}','{$unit}','{$note}')");
		echo "<script language=javascript>
			alert('新建成功！');
			window.location='".base_url()."';
			</script>";
	}
	function modify_dhcp(){ //修改
		$id = $this->input->post('id');
		$host = $this->input->post('host');
		$mac = $this->input->post('mac');
		$ip = $this->input->post('ip');
		$name = $this->input->post('name');
		$computer = $this->input->post('computer');
		$unit = $this->input->post('unit');
		$note = $this->input->post('note');
		$sql = "select * from dhcp where id<>'{$id}'";
		$query = $this->db->query($sql);
		foreach($query->result_array() as $row){
			if($row['host']==$host){
				echo "<script language=javascript>
					alert('主机名已存在');
					window.location='".base_url()."net_dhcp/modify/".$id."';
					</script>";
				exit();
			}elseif($row['mac']==$mac){
				echo "<script language=javascript>
					alert('mac地址已存在');
					window.location='".base_url()."net_dhcp/modify/".$id."';
					</script>";
					exit();
			}elseif($row['ip']==$ip){
				echo "<script language=javascript>
					alert('ip地址已存在');
					window.location='".base_url()."net_dhcp/modify/".$id."';
					</script>";
					exit();
			}
		}
		$this->db->query("update dhcp set host='{$host}',mac='{$mac}',ip='{$ip}',name='{$name}',computer_name='{$computer}',unit='{$unit}',note='{$note}' where id='{$id}'");
		echo "<script language=javascript>
			alert('修改成功！');
			window.location='".base_url()."';
			</script>";
	}
	function del_dhcp($id){
		$sql = "delete from dhcp where id='{$id}'";
		$this->db->query($sql);
		echo "<script language=javascript>
			alert('删除成功！');
			window.location='".base_url()."';
			</script>";
	}
	function download(){
		$query = $this->db->query('select * from dhcp order by id desc');
		return $query->result_array();
	}
}
?>